package com.kh.statistics.video.core.statistics;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kh.statistics.video.constant.PlatformConstant;
import com.kh.statistics.video.core.MediaContext;
import com.kh.statistics.video.core.Platform;
import com.kh.statistics.video.entity.StatisticsInfo;
import com.kh.statistics.video.entity.StatisticsMeta;
import com.kh.statistics.video.entity.StatisticsOne;
import com.kh.statistics.video.helper.ExcelHelper;
import com.kh.statistics.video.helper.StatisticsHttpHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @BelongsProject: statistics
 * @BelongsPackage: com.kh.statistics.video.core.statistics
 * @Author: Hong.Kong
 * @CreateTime: 2024-05-10  09:41
 * @Description: TODO
 * @Version: 1.0
 */
@Component
@Slf4j
public class DongCheDiPlatform implements Platform {
    @Override
    public String support() {
        return PlatformConstant.DONG_CHE_DI;
    }

    @Override
    public List<StatisticsInfo> getInfo(StatisticsMeta statisticsMeta) {
        return null;
    }

    @Override
    public void statistics(List<StatisticsMeta> list) {
        List<StatisticsInfo> statisticsInfoListAll = new ArrayList<>();
        for (StatisticsMeta statisticsMeta : list) {
            StatisticsInfo statisticsInfo = new StatisticsInfo();
            if ("keyMetrics".equals(statisticsMeta.getName())) {
            }
            statisticsInfoListAll.add(statisticsInfo);
        }
        ExcelHelper.export(ExcelHelper.getExcelName(list.get(0).getAccountName(), "懂车帝统计总"), statisticsInfoListAll, StatisticsInfo.class);
    }

    @Override
    public void oneStatistics(List<StatisticsMeta> statisticsMetaList, MediaContext mediaContext) {
        List<StatisticsOne> statisticsInfoListAll = new ArrayList<>();
        Optional<StatisticsMeta> statistics = statisticsMetaList.stream().filter((item) -> null != item.getName() && "oneStatistics".equals(item.getName())).findFirst();
        if (statistics.isPresent()) {
            StatisticsMeta statisticsMeta = statistics.get();
            Map<String, String> headers = statisticsMeta.getHeaders();
            headers.put("referer", "https://mp.dcdapp.com/profile_v2/manage/content/short-video");
            String body = StatisticsHttpHelper.request(statisticsMeta);
            if (!handlerResponse(body)) {
                return;
            }
            List<StatisticsOne> statisticsInfos = oneStatistics(body);
            statisticsInfoListAll.addAll(statisticsInfos);
        }
        ExcelHelper.export(ExcelHelper.getExcelName(statisticsMetaList.get(0).getAccountName(), "懂车帝单条统计"), statisticsInfoListAll, StatisticsOne.class);
    }

    @Override
    public boolean handlerResponse(String body) {
        return true;
    }

    private List<StatisticsOne> oneStatistics(String body) {
        List<StatisticsOne> itemList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray array = data.getJSONArray("list");
        for (Object o : array) {
            StatisticsOne statisticsOne = new StatisticsOne();
            JSONObject item = (JSONObject) o;
            String title = item.getString("title");
            statisticsOne.setWorks(title);
            String create_time = item.getString("create_time");
            statisticsOne.setPushDate(create_time);
            String share_url = item.getString("share_url");
            statisticsOne.setLink(share_url);
            Long play_count = item.getLong("play_count");
            statisticsOne.setExposure(play_count);
            Long likeCount = item.getLong("digg_count");
            statisticsOne.setClicksNumber(likeCount);
            Long commentCount = item.getLong("comment_count");
            statisticsOne.setCommentsNumber(commentCount);
            itemList.add(statisticsOne);
        }
        return itemList;
    }
}
